package com.practice.listener;

import com.practice.config.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author:高杰领
 * @date:2021/11/18 11:12
 * @description:在servlet容器初始化时,启动spring容器
 * @version:1.0
 **/
//@WebListener
public class SpringContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("servlet容器初始化了");
        ServletContext servletContext = sce.getServletContext();
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(SpringConfig.class);
        applicationContext.refresh();
        servletContext.setAttribute("applicationContext",applicationContext);
    }
}
